package com.dao.impl;

import com.dao.BaseDao;
import com.dao.intf.BlogDao;
import com.dao.intf.DraftDao;
import com.entity.Draft;

import java.util.List;

public class DraftDaoImpl extends BaseDao implements DraftDao {
//    @Override
//    public int queryDraftId(Integer userId, String draft) {
//        String sql=" SELECT  `id` FROM `draft` WHERE `userId`=? AND `draft`=?";
//        Integer draftId = (Integer) queryForSingleValue(sql, userId, draft);
//        return draftId;
//    }

    @Override
    public List<Draft> queryDraftByUserId(Integer userId) {
        String sql="SELECT `id`, `draft` FROM `draft` WHERE `userId`=?";


        return  queryForList(Draft.class,sql,userId);
    }

    @Override
    public Draft querySingleDraftByDraftId(Integer draftId) {
        String sql="SELECT  `userId` ,`draft` FROM `draft` WHERE `id`=?";
        return queryForOne(Draft.class,sql,draftId);
    }


    @Override
    public int saveDraft(Integer userId, String draft) {
        String sql=" INSERT INTO `draft`(`userId`,`draft`) VALUES(?,?)";
        return  update(sql,userId,draft);

    }

    @Override
    public int releaseDraft(Integer draftId) {

        //把草稿内容保存到博客表
        Draft draft = querySingleDraftByDraftId(draftId);
        System.out.println(draft);

        //删除草稿表里的相应的草稿内容
        String sql1="DELETE FROM `draft` WHERE `id` = ?";
        update(sql1,draftId);

        String sql="INSERT INTO `blog` (`userId`,`content`,`favour`) VALUES(?,?,0)";
        return  update(sql,draft.getUserId(),draft.getDraft());

    }
}
